<?php
include('include/config.inc.php');
if(!isset($_SESSION['s_activId'.$cfgPack])
  || (isset($_SESSION['s_userType'.$cfgPack]) && isset($cfgFileNames['technician'][$_SESSION['s_userType'.$cfgPack]]) 
      && $cfgFileNames['technician'][$_SESSION['s_userType'.$cfgPack]] == 0))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{
	if(isset($_REQUEST['saveBtn']))
	{
		
		foreach($_REQUEST['currentMaxLevel'] as $maxKey=> $maxValue)
		{
			$updateMaxLevel = "UPDATE item 
			                       SET maxLevel = '".$maxValue."' 
			                     WHERE itemId  = ".$maxKey;
      $updateMaxLevelRes = mysql_query($updateMaxLevel);
      if(!$updateMaxLevelRes)
        die("Query not Updated : ".mysql_error());
		}
		foreach($_REQUEST['currentMinLevel'] as $minKey=> $minValue)
		{
			$updateMinLevel = "UPDATE item 
			                       SET minLevel = '".$minValue."' 
			                     WHERE itemId  = ".$minKey;
      $updateMinLevelRes = mysql_query($updateMinLevel);
      if(!$updateMinLevelRes)
        die("Query not Updated : ".mysql_error());
		}
		foreach($_REQUEST['currentStock'] as $key=> $value)
		{
			$updateItemStock = "UPDATE item 
			                       SET currentStock = '".$value."' 
			                     WHERE itemId  = ".$key;
      $updateItemStockRes = mysql_query($updateItemStock);
      if(!$updateItemStockRes)
        die("Query not Updated : ".mysql_error());
      else
        header("Location:productList.php");
		}
	}
	
	$itemDetailArray     = array();
	$categoryIdArr       = array();
	$categoryNameArr     = array();
	$subCategoryIdArr    = array();
	$subCategoryNameArr  = array();
	$itemNameArr         = array();
	$itemIdArr           = array();
  $itemName            = "";
  $itemMake            = "";
  $modelNo             = "";
  $i                   = 0;  
  $itemId              = isset($_REQUEST['itemId']) ? $_REQUEST['itemId'] : 0;
  
  $selectItem = "SELECT item.itemId,item.categoryId,item.subCategoryId,item.itemName,item.itemMake,item.modelNo,item.currentStock,item.maxLevel,item.minLevel,
                        category.categoryId,category.categoryName,subcategory.subCategoryId,subcategory.subCategoryName
                   FROM item
                   JOIN category
                   JOIN subcategory
                  WHERE item.categoryId = category.categoryId
                    AND item.subCategoryId = subcategory.subCategoryId
               ORDER BY itemName";
  $selectItemResult = mysql_query($selectItem);
  $itemRow = mysql_num_rows($selectItemResult);
  while($itemRow = mysql_fetch_array($selectItemResult))
  {
    $itemDetailArray[$i]['itemId']          = $itemRow['itemId'];
    $itemDetailArray[$i]['categoryName']    = $itemRow['categoryName'];
    $itemDetailArray[$i]['subCategoryName'] = $itemRow['subCategoryName'];
    $itemDetailArray[$i]['itemName']        = $itemRow['itemName'];
    $itemDetailArray[$i]['itemMake']        = $itemRow['itemMake'];
    $itemDetailArray[$i]['modelNo']         = $itemRow['modelNo'];
    $itemDetailArray[$i]['currentStock']    = $itemRow['currentStock'];
    $itemDetailArray[$i]['maxLevel']        = $itemRow['maxLevel'];
    $itemDetailArray[$i]['minLevel']        = $itemRow['minLevel'];
    
    if($itemRow['itemId'] == $itemId)
    {
      $itemName  = $itemRow['itemName'];
      $itemMake  = $itemRow['itemMake'];
      $modelNo   = $itemRow['modelNo'];
    }
    $i++;     
  }
 // ==================== Category =========================================
  $selectCategoryQuery = "SELECT categoryId,categoryName
	                          FROM category
	                      ORDER BY categoryName";
	$selectCategoryQueryRes = mysql_query($selectCategoryQuery);
	$j = 0;
	while($categoryRow = mysql_fetch_array($selectCategoryQueryRes))
	{
	  if($j == 0 || (isset($_REQUEST['categoryId']) && $_REQUEST['categoryId'] == $categoryRow['categoryId']))
	    $categorySelected = $categoryRow['categoryId'];
	
	  $categoryIdArr[$j]     = $categoryRow['categoryId'];
	  $categoryNameArr[$j]   = $categoryRow['categoryName'];
	  $j++;
	}
	//=========================== Sub Category  ==================================
	$selectSubCategoryQuery = "SELECT subCategoryId,subCategoryName
	                             FROM subcategory
	                         ORDER BY subCategoryName";
	$selectSubCategoryQueryRes = mysql_query($selectSubCategoryQuery);
	$c = 0;
	while($subCategoryRow = mysql_fetch_array($selectSubCategoryQueryRes))
	{
	  if($c == 0 || (isset($_REQUEST['subCategoryId']) && $_REQUEST['subCategoryId'] == $subCategoryRow['subCategoryId']))
	    $subCategorySelected = $subCategoryRow['subCategoryId'];
	
	  $subCategoryIdArr[$c]     = $subCategoryRow['subCategoryId'];
	  $subCategoryNameArr[$c]   = $subCategoryRow['subCategoryName'];
	  $c++;
	}
	//=========================Product Name =============================
	$itemQuery = "SELECT itemId,itemName
	                FROM item
	            ORDER BY itemName";
	$selectItemQueryRes = mysql_query($itemQuery);
	$a = 0;
	while($itemRow = mysql_fetch_array($selectItemQueryRes))
	{
	  if($a == 0 || (isset($_REQUEST['itemId']) && $_REQUEST['itemId'] == $itemRow['itemId']))
	    $itemSelected = $itemRow['itemId'];
	
	  $itemIdArr[$a]     = $itemRow['itemId'];
	  $itemNameArr[$a]   = $itemRow['itemName'];
	  $a++;
	}
  
  include("./bottom.php");
  $smarty->assign("itemId",$itemId);
  $smarty->assign("itemName",$itemName);
  $smarty->assign("itemMake",$itemMake);
  $smarty->assign("modelNo",$modelNo);
  $smarty->assign("itemDetailArray",$itemDetailArray);
  $smarty->assign("categoryIdArr",$categoryIdArr);
  $smarty->assign("categoryNameArr",$categoryNameArr);
  $smarty->assign("subCategoryIdArr",$subCategoryIdArr);
  $smarty->assign("subCategoryNameArr",$subCategoryNameArr);
  $smarty->assign("itemNameArr",$itemNameArr);
  $smarty->assign("itemIdArr",$itemIdArr);
  $smarty->assign("i",$i);
  $smarty->display("productList.tpl");
}
?>